package com.firechat.webclient.session.data;

import com.firechat.webclient.session.action.ServerActions;
import org.json.simple.JSONObject;

import java.util.UUID;

public class PackageBuilder {


    //用户名密码
    public static String buildLogin(String username, String password){
        JSONObject data = new JSONObject();
        data.put("username",username);
        data.put("password",password);

        return pack(ServerActions.Login, data);
    }


    /**
     * 封装数据包
     * @param type 包类型
     * @param data 包数据
     * @return 打包的字符串
     */
    public static String pack(ServerActions type, JSONObject data){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Id", UUID.randomUUID().toString());
        jsonObject.put("Action", type.toString());
        jsonObject.put("Data", data);
        return jsonObject.toString();
    }

    /**
     * 解析数据包
     * @param jsonString 数据包Json字符串
     * @return 接收到的包
     */
    public static ReceivedPackage unpack(String jsonString){

        return new ReceivedPackage();
        /*ReceivePackage packge = new ReceivePackage();
        packge.setId((String) jsonObject.get("Id"));
        packge.setData(jsonObject.get("Data"));
        packge.setAction((String) jsonObject.get("Action"));
        packge.setMessage((String) jsonObject.get("Message"));
        packge.setIsSuccess((Boolean) jsonObject.get("IsSuccess"));
        return packge;

        public void Unpack(JSONObject jsonObject){
        packge.setId((String) jsonObject.get("Id"));
        packge.setData(jsonObject.get("Data"));
        packge.setAction((String) jsonObject.get("Action"));
        packge.setMessage((String) jsonObject.get("Message"));
        packge.setIsSuccess((Boolean) jsonObject.get("IsSuccess"));
    }




        */
    }
}
